﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

/// <summary>
/// Summary description for XMLWrite
/// </summary>
public class XMLWrite
{

    public static string Serialize(object objectToSerialize)
    {
        XmlSerializer serializer = new XmlSerializer(objectToSerialize.GetType());
        StringWriter writer = new StringWriter();
        serializer.Serialize(writer, objectToSerialize);
        return writer.ToString();
    }

    public static object Deserialize<T>(string xmlString) where T : new()
    {
        T objectType = new T();
        StringReader StrReader = new StringReader(xmlString);
        XmlSerializer Xml_Serializer = new XmlSerializer(objectType.GetType());
        XmlTextReader XmlReader = new XmlTextReader(StrReader);
        try
        {
            Object AnObject = Xml_Serializer.Deserialize(XmlReader);
            return AnObject;
        }
        finally
        {
            XmlReader.Close();
            StrReader.Close();
        }
    }
}